function Promise(executor) {
    //初始化promise的两个值
    this.PromiseState = 'pending';
    this.PromiseResult = null;
    let self = this;
    //执行多个回调要用数组
    this.callbacks = [];

    //创建resolve函数
    function resolve(data) {
        //状态只能修改一次的代码实现
        if (self.PromiseState !== 'pending') return;
        //修改promise的状态
        self.PromiseState = 'fulfilled';
        //修改promise的值
        self.PromiseResult = data;
        //思路:在一异步期间 将then的回调函数保存下来 等到状态改变的时候调用
        self.callbacks.forEach(item => {
            item.onResolve(data);
        })
    }

    //创建reject函数
    function reject(data) {
        //状态只能修改一次的代码实现
        if (self.PromiseState !== 'pending') return;
        //修改promise的状态
        self.PromiseState = 'rejected';
        //修改promise的值
        self.PromiseResult = data;
        self.callbacks.forEach(item => {
            item.onReject(data);
        })
    }

    //实现抛出异常改变promise状态的实现
    try {
        //调用执行器函数 传入两个参数
        executor(resolve, reject);
    } catch (e) {
        reject(e);//直接调用reject就行 把抛出的内容传给他
    }


}
// then方法的实现
Promise.prototype.then = function (onResolve, onReject) {
    if (this.PromiseState === 'fulfilled') {
        onResolve(this.PromiseResult);
    }
    if (this.PromiseState === 'rejected') {
        onReject(this.PromiseResult);
    }
    //异步的进行方式 保存两个函数
    if (this.PromiseState === 'pending') {
        this.callbacks.push({
            onReject:onReject,
            onResolve:onResolve
        })
    }
}